#!/usr/bin/env python
# coding: utf-8

# In[1]:


##
#让解释器支持多个数字加减法表达式的解释


# In[2]:


INTEGER, PLUS, MINUS, EOF = 'INTERGER', 'PLUS', 'MINUS', 'EOF'


# In[3]:


class Token:
    """记号类
    """
    def __init__(self, value_type, value):
        self.value_type = value_type # 记号的类型
        self.value = value # 记号的值
    
    def __str__(self):
        """重写查看记号内容的方法"""
        return f"Token({self.value_type}, {self.value})"
    
    def __repr__(self):
        return self.__str__()


# In[4]:


class Interpreter:
    """解释器类
    """
    def __init__(self, text):
        self.text = text
        self.position = 0
        self.current_token = None
        self.current_char = self.text[self.position]
    
    def error(self):
        raise Exception('警告：错误的输入内容！')
    
    def advance(self):
        """定义获取下一个字符的方法
        """
        self.position += 1
        if self.position >= len(self.text):
            self.current_char = None
        else:
            self.current_char = self.text[self.position]
    
    def skip_whitespace(self):
        """定义跳过空格的方法
        """
        while self.current_char is not None and self.current_char.isspace():
            self.advance()
    
    def long_integer(self):
        result = ''
        while self.current_char is not None and self.current_char.isdigit():
            result += self.current_char
            self.advance()
        return int(result)
            
    def get_next_token(self):
        while self.current_char is not None:
            if self.current_char.isspace():
                self.skip_whitespace()
                continue
            if self.current_char.isdigit():
                token = Token(INTEGER, self.long_integer())
                return token
            if self.current_char == '+':
                token = Token(PLUS, self.current_char)
                self.advance()
                return token
            if self.current_char == '-':
                token = Token(MINUS, self.current_char)
                self.advance()
                return token
            self.error()
        return Token(EOF, None)
    
    def eat(self, token_type):
        print(self.current_token)
        if self.current_token.value_type == token_type:
            if token_type != EOF:
                self.current_token = self.get_next_token()
        else:
            self.error()
    
    def term(self):
        token = self.current_token
        self.eat(INTEGER)
        return token.value
    
    def expr(self):
        self.current_token = self.get_next_token()
        result = self.term()
        
        while self.current_token.value_type in (PLUS, MINUS):
            if self.current_token.value_type == PLUS:
                self.eat(PLUS)
                result += self.term()
            if self.current_token.value_type == MINUS:
                self.eat(MINUS)
                result -= self.term()
        return result
    


# In[5]:


def main():
    while True:
        text = input("输入算式：")
        if text == 'q':
            break
        interpreter = Interpreter(text)
        result = interpreter.expr()
        print(text, "=", result)


# In[6]:


main()

